<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\MySetService;
use Illuminate\Http\Request;

class MySetController extends BaseController
{
    //用户设置

    public function __construct(MySetService $service)
    {
        $this->db = $service;
    }

    //价格列表
    public function priceList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->priceList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 收费修改
     */
    public function modifyPrice(Request $request)
    {
        $token = $request->post('token', '');
        $seek = $request->post('seek_price', 0);
        $daibans = $request->post('daibans', []); //id、title、price
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$seek) {
            return $this->toJson(-2, '咨询费必须的');
        }
        if (!$daibans) {
            return $this->toJson(-2, '代办费必须的');
        }
        if (!is_array($daibans)) {
            return $this->toJson(-2, '代办费格式错误');
        }
        $res = $this->db->priceModify($uid, $seek, $daibans);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //代办列表
    public function daibanList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->daibanList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //代办修改
    public function modifyDaiban(Request $request)
    {
        $token = $request->post('token', '');
        $daiban_list = $request->post('daiban_list', []);
        $yy_list = $request->post('yiyuan_list', []);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (empty($daiban_list)) {
            return $this->toJson(-2, '代办必须的');
        }
        if (empty($yy_list)) {
            return $this->toJson(-2, '医院必须的');
        }
        $res = $this->db->daibanModify($uid, $daiban_list, $yy_list);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //医护列表
    public function yihuList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->yihuList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //医护修改
    public function modifyYihu(Request $request)
    {
        $token = $request->post('token', '');
        $yihu_list = $request->post('yihu_list', []);
        $yihu_time = $request->post('yihu_time', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (empty($yihu_list)) {
            return $this->toJson(-2, '医护须的');
        }
        if (!$yihu_time) {
            return $this->toJson(-2, '可上门时间必须的');
        }
        $res = $this->db->yihuModify($uid, $yihu_list, $yihu_time);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //医护自定义
    public function yihuAdd(Request $request)
    {
        $token = $request->post('token', '');
        $pas['cate_name'] = $request->post('title', '');
        $pas['price'] = $request->post('price', 0);
        $pas['detail'] = $request->post('detail', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['cate_name']) {
            return $this->toJson(-2, '医护名称必须的');
        }
        if (!$pas['price']) {
            return $this->toJson(-2, '价格必须的');
        }
        $res = $this->db->yihuAdd($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //陪诊列表
    public function peizhenList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->peizhenList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //陪诊设置
    public function peizhenModify(Request $request)
    {
        $token = $request->post('token', '');
        $yys = $request->post('yiyuan_list', []);
        $pz_time = $request->post('peizhen_time', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!count($yys)) {
            return $this->toJson(-2, '医院必须的');
        }
        if (!$pz_time) {
            return $this->toJson(-2, '陪诊时间必须的');
        }
        $res = $this->db->peizhenModify($uid, $yys, $pz_time);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}